package com.zjw.zy.coreui.fragment

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zjw.zy.R
import com.zjw.zy.base.BaseFragment
import com.zjw.zy.base.BaseRecyclerAdapter
import com.zjw.zy.bean.RecorderItem
import com.zjw.zy.coreui.dialog.LoadingDialog
import com.zjw.zy.coreui.fragment.adapter.RecorderAdapter
import com.zjw.zy.coreui.fragment.holder.RecorderItemHolder
import com.zjw.zy.coreui.view.RecorderButton
import com.zjw.zy.ext.alertConfirm
import com.zjw.zy.ext.mainActivity
import com.zjw.zy.manager.PageManager
import com.zjw.zy.utils.ResourcesUtils
import com.zjw.zy.utils.TimeUtils
import com.zjw.zy.utils.WindowUtils
import com.zjw.zy.viewmodel.RecorderViewModel

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/10/17 15:45
 * @description：录音转文字
 */
class RecorderFragment : BaseFragment() {

    private val mRecorderViewModel: RecorderViewModel by viewModels()

    private val mRecorderTime: TextView by lazy { findViewById(R.id.fragment_recorder_time) }
    private val mRecorderName: TextView by lazy { findViewById(R.id.fragment_recorder_name) }
    private val mList: RecyclerView by lazy { findViewById(R.id.fragment_recorder_list) }
    private val mRecorderButton: RecorderButton by lazy { findViewById(R.id.fragment_recorder_button) }

    private var mPlayingRecorderItemHolder: RecorderItemHolder? = null
    private val mAdapter: RecorderAdapter =
        RecorderAdapter(object : BaseRecyclerAdapter.ItemListener<RecorderItemHolder> {
            override fun onCallbackOtherEvent(event: String, vararg args: Any?) {
                when (event) {
                    RecorderItemHolder.EVENT_PLAY -> {
                        mPlayingRecorderItemHolder = args[0] as RecorderItemHolder
                        mPlayingRecorderItemHolder?.let {
                            mRecorderViewModel.startPlay(it.data)
                        }
                    }
                    RecorderItemHolder.EVENT_STOP -> {
                        mRecorderViewModel.pausePlay()
                    }

                    RecorderItemHolder.EVENT_DELETE -> {
                        val recorderItem = args[0] as RecorderItem
                        mRecorderViewModel.delete(recorderItem)
                    }

                    RecorderItemHolder.EVENT_RENAME -> {
                        val recorderItem = args[0] as RecorderItem
                        mRecorderViewModel.updateItem(recorderItem)
                    }
                    RecorderItemHolder.EVENT_TRANS_TO_TEXT -> {
                        val recorderItem = args[0] as RecorderItem
                        val tips =
                            LoadingDialog(mainActivity()).apply {
                                title(ResourcesUtils.getString(R.string.recorder_loading_txt))
                                show()
                            }
                        mRecorderViewModel.requestVoiceToText(recorderItem.path) {
                            tips.dismiss()
                            PageManager.goText(it, null)
                        }
                    }
                }
            }

        })

    override fun onPageEnter() {
        WindowUtils.TopBar.showBack()
        WindowUtils.FootBar.hide()
        WindowUtils.TopBar.setTopBarLabelText(getString(R.string.recorder_fragment_label))
        WindowUtils.TopBar.show1RightButton(ResourcesUtils.getDrawable(R.drawable.icon_delete)) {
            alertConfirm(message = ResourcesUtils.getString(R.string.delete_all_confirm)) {
                if (it) {
                    mRecorderViewModel.deleteAll()
                }
            }
        }
        mRecorderViewModel.refreshRecorderData()
    }

    override fun onViewRestore(savedInstanceState: Bundle?) {

    }

    override fun onSubscribe() {
        mRecorderViewModel.getTimeRecorder().observe(this) {
            mRecorderTime.text = getString(
                R.string.recorder_fragment_time_str,
                TimeUtils.adjustTime(it / 1000 / 60),
                TimeUtils.adjustTime(it / 1000 % 60),
                TimeUtils.adjustTime(it % 1000 / 10),
            )
        }

        mRecorderViewModel.getTimePlayer().observe(this) {
            val current = it.first
            val total = it.second
            mPlayingRecorderItemHolder?.updateProgress(current)
        }
        mRecorderViewModel.getRecorderData().observe(this) {
            mAdapter.notifyDataWithDiff(it)
        }
    }

    override fun onBindViewListener() {
        mRecorderButton.bindListener(object : RecorderButton.ModeListener {
            override fun onModeChange(mode: Int) {
                if (mode == RecorderButton.MODE_RECORDER) {
                    recorderModeStart()
                } else {
                    recorderModeEnd()
                }
            }
        })

    }

    private fun recorderModeEnd() {
        mRecorderViewModel.stopRecorder()
        mRecorderName.visibility = View.GONE
        mRecorderTime.visibility = View.GONE
        mRecorderViewModel.refreshRecorderData()
    }

    fun recorderModeStart() {
        //录制模式
        mRecorderName.visibility = View.VISIBLE
        mRecorderTime.visibility = View.VISIBLE
        mRecorderViewModel.startRecorder()
    }

    override fun onBindViewValue() {
        mList.adapter = mAdapter;
        mList.layoutManager = LinearLayoutManager(mainActivity())
        mRecorderTime.text = getString(R.string.recorder_fragment_time_str, "00", "00", "00")
    }

    override fun onFindView(rootView: View?) {

    }

    override fun onLayoutId(): Int {
        return R.layout.fragment_recorder
    }

    override fun onPageExit() {
        mPlayingRecorderItemHolder = null
    }
}